package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemdictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService CustomerService;
    @Autowired
    private ISystemdictionaryItemService itemService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ICustomerTraceHistoryService historyService;


    @RequiresPermissions(value={"客户列表","customer:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        qo.setStatus(Customer.STATUS_POTENTIAL);
        model.addAttribute("pageInfo", CustomerService.query(qo));
        //查找所有的客户工作
        model.addAttribute("jobs", itemService.listItemByDicSn("job"));
        model.addAttribute("sources", itemService.listItemByDicSn("source"));
        model.addAttribute("cmds", itemService.listItemByDicSn("communicationMethod"));
        model.addAttribute("sellers", employeeService.listSellersByRoleSns("Market_Manager","Market"));
        //historyService
        return "customer/list";
    }

    @RequiresPermissions(value={"客户保存或更新","customer:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer entity) {

        JsonResult result = new JsonResult();
        try {
            //int a=1/0;
            CustomerService.saveOrUpdate(entity);
        }catch (Exception e)
        {
           e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }
    @RequiresPermissions(value={"客户更新状态","customer:updateStatus"},logical = Logical.OR)
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long cid, Integer status) {

        JsonResult result = new JsonResult();
        try {
            //int a=1/0;
            CustomerService.updateStatus(cid,status); //entity包括id name status
        }catch (Exception e)
        {
           e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

    @RequiresPermissions(value={"客户池列表","customer:poolList"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String PoolList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        qo.setStatus(Customer.STATUS_POOLED);
        model.addAttribute("pageInfo", CustomerService.query(qo));
        //查找所有的客户工作
        model.addAttribute("jobs", itemService.listItemByDicSn("job"));
        model.addAttribute("sources", itemService.listItemByDicSn("source"));
        model.addAttribute("sellers", employeeService.listSellersByRoleSns("Market_Manager","Market"));
        return "/customer/poolList";
    }

}
